package com.egg.ylt.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.fragment.FRA_GrowUpCurve;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.log.Elog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lijiateng on 2019/3/4.
 */

public class ACT_WaterAirDetail extends BaseActivity {

    public static final int TYPE_WATER = 1;
    public static final int TYPE_AIR = 2;

    public static final int FEATURE_WATER_PH = 1;
    public static final int FEATURE_WATER_DIRTY = 2;
    public static final int FEATURE_WATER_TEMPER = 3;

    public static final int FEATURE_AIR_PM = 1;
    public static final int FEATURE_AIR_TEMPER = 2;
    public static final int FEATURE_AIR_MOIST = 3;
    public static final int FEATURE_AIR_POLLUTE = 4;

    @BindView(R.id.rl_include_title)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_tv_right)
    TextView includeTvRight;
    @BindView(R.id.include_iv_right)
    ImageView includeIvRight;
    @BindView(R.id.wv_air_water)
    WebView wvAirWater;

    private int mType;
    private int mFeature;
    private String mShopId;

    public static void startAct(Context context, String shopId, int type, int feature) {
        Intent intent = new Intent(context, ACT_WaterAirDetail.class);
        intent.putExtra("shopId", shopId);
        intent.putExtra("type", type);
        intent.putExtra("feature", feature);
        context.startActivity(intent);
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        mShopId = extras.getString("shopId");
        mType = extras.getInt("type");
        mFeature = extras.getInt("feature");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_water_air_detail;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_style_orange);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        includeTvTitle.setText(getActTitle());
        includeTvRight.setVisibility(View.GONE);

        initWebView();

        String url = String.format(
                API.BASE_WEB_URL+"?companyId=%s&shopId=%s&type=%s&feature=%s#/air",
                Constants.COMPANYID, mShopId, mType, mFeature);

        Elog.e("ljt", url);

        wvAirWater.loadUrl(url);

    }


    private void initWebView() {

        WebSettings settings = wvAirWater.getSettings();
        settings.setUseWideViewPort(true);

        wvAirWater.getSettings().setJavaScriptEnabled(true);

        wvAirWater.setWebChromeClient(new WebChromeClient());
        wvAirWater.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                String url = String.format("http://test.mumzone.cn:94/#/test?type=%s&token=%s&babyId=%s",
//                        curveType, Constants.TOKEN, babyId);
//                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                toggleShowDialogLoading("加载中...");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissDialogLoad();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

    }

    private String getActTitle() {
        StringBuffer sbTitle = new StringBuffer();
        switch (mType) {
            case TYPE_WATER:
                sbTitle.append("水质 - ");
                switch (mFeature) {
                    case FEATURE_WATER_PH:
                        sbTitle.append("ph");
                        break;
                    case FEATURE_WATER_DIRTY:
                        sbTitle.append("浑浊度");
                        break;
                    case FEATURE_WATER_TEMPER:
                        sbTitle.append("水温");
                        break;
                }
                break;
            case TYPE_AIR:
                sbTitle.append("空气 - ");
                switch (mFeature) {
                    case FEATURE_AIR_PM:
                        sbTitle.append("pm2.5");
                        break;
                    case FEATURE_AIR_TEMPER:
                        sbTitle.append("温度");
                        break;
                    case FEATURE_AIR_MOIST:
                        sbTitle.append("湿度");
                        break;
                    case FEATURE_AIR_POLLUTE:
                        sbTitle.append("甲醛");
                        break;
                }
                break;
        }

        return sbTitle.toString();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

}
